package com.ym.pms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.pms.dao.BunkMapper;
import com.ym.pms.dao.RoomMapper;
import com.ym.pms.dao.SuiteMapper;
import com.ym.pms.getCoding.NewCoding;
import com.ym.pms.model.Room;
import com.ym.pms.service.BunkService;
import com.ym.pms.service.RoomService;
import com.ym.pms.service.SuiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    RoomMapper rmDao;
    @Autowired
    SuiteMapper suDao;
    @Autowired
    NewCoding newCoding;
    @Autowired
    BunkMapper bkDao ;
    @Autowired
    BunkService bunkService ;

    @Override
    public int add(Room room) {
        room.setRmCoding(getCoding());
        Room room1 = suDao.getRmSu(room.getRmSucoding());
        room.setRmItid(room1.getRmItid());
        room.setRmBucoding(room1.getRmBucoding());
        room.setRmRegion(room1.getRmRegion());
        room.setRmFloor(room1.getRmFloor());
        room.setGender("无");
        if (suDao.getSuEstate(room.getRmSucoding()).equals("STARTUSING")) {
            room.setRmSouse("VACANT");
            if(suDao.getSUSOUse(room.getRmSucoding()).equals("LOCKING") ){
                suDao.changeSuSouse(room.getRmCoding(), "USING");
            }
        } else {
            room.setRmSouse("LOCKING");
        }
        room.setRmHavebunk("NO");
        int i = rmDao.insertSelective(room);
        return i;
    }

    @Override
    public int delete(String rmCoding) {
        if (rmCoding == null || rmCoding.equals("")) {
            return -1;
        } else if (findByCoding(rmCoding) == null) {
            return -2;
        } else {
            bunkService.deleteAllByRmCoding(rmCoding);
            return rmDao.deleteByPrimaryKey(rmCoding);
        }
    }

    @Override
    public int update(Room room) {
        if (room.getRmCoding() == null || room.getRmCoding().equals("")) {
            return -1;
        } else if (findByCoding(room.getRmCoding()) == null) {
            return -2;
        } else {
            return rmDao.updateByPrimaryKeySelective(room);
        }
    }

    @Override
    public Room findByCoding(String rmCoding) {
        return rmDao.selectByPrimaryKey(rmCoding);
    }

    @Override
    public PageInfo findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Room> rooms = rmDao.queryRoomList();
        PageInfo pageInfo = new PageInfo<Room>(rooms);
        return pageInfo;
    }

    @Override
    public PageInfo findByGender(String gender, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Room> rooms = rmDao.findByGender(gender);
        PageInfo pageInfo = new PageInfo<Room>(rooms);
        return pageInfo;
    }

    @Override
    public PageInfo fuzzyQuery(Room room, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Room> rooms = rmDao.fuzzyQuery(room);
        PageInfo pageInfo = new PageInfo<Room>(rooms);
        return pageInfo;
    }

    @Override
    public int getMaxNo() {
        Integer BuildingMaxNo = rmDao.getBuildingMaxNo();
        if (BuildingMaxNo == null) {
            return 0;
        } else {
            return BuildingMaxNo;
        }
    }

    @Override
    public String getCoding() {
        Integer maxNo = this.getMaxNo();
        maxNo += 1;
        String suCoding = newCoding.getCoding("RM", maxNo);
        return suCoding;
    }

    @Override
    public void changeSatus(String rmCoding) {
        Room room = rmDao.selectByPrimaryKey(rmCoding);
        if(bkDao.findAllInRoom(room.getRmCoding()).size() == 0){
            rmDao.changeStatus(rmCoding,"LOCKING");
        }else if(bkDao.findAllInRoom(room.getRmCoding()).size() < room.getRmBkno()){
            rmDao.changeStatus(rmCoding,"USING");
        }
    }

    @Override
    public void deleteAllBySuCoding(String id) {
        List<String> allCoding = rmDao.getAllCoding(id);
        for (String s : allCoding) {
            bunkService.deleteAllByRmCoding(s);
        }
        rmDao.deleteAllBySuCoding(id);
    }
}
